<?php
require_once("global.php");
$navid="my";
if(!USER_STATUS){
    Error('请登录','login.php');
}


if(!$act)
{

    $msg_img = $DB->qgGetOne("SELECT * FROM ".$prefix."upfiles WHERE id=".$_SESSION["qg_sys_user"]["thumb"]);
	$act = "home";
}


if($act=="home")
{
	FOOT("my.home");
}elseif($act=="add_msg"){//添加（编辑）新闻
    //文章分类
    $msg_cat_list = $DB->qgGetAll("SELECT * FROM ".$prefix."category WHERE parentid='17' and status ='1' ");
    if($id){//编辑是获取新闻内容信息
        $msg_info = $DB->qgGetOne("SELECT * FROM ".$prefix."msg WHERE id=".$id);
        $msg_content= $DB->qgGetOne("SELECT * FROM ".$prefix."msg_content WHERE id=".$id);
        $msg_images= $DB->qgGetOne("SELECT * FROM ".$prefix."upfiles WHERE id=".$msg_info['thumb']);
    }
    FOOT("my.msg");
}
elseif($act=='uploadimg'){//我的新闻列表


    $file = $_FILES['file'];//得到传输的数据

//得到文件名称
    $name = $file['name'];

    if($name)
    {
        $filetype = strtolower(substr($name,strrpos($name,'.')+1)); //得到文件类型，并且都转化成小写
        $allow_type = array('jpg','png','jpeg');//定义允许上传的类型

        //判断文件类型是否被允许上传
        if(!in_array($filetype, $allow_type))
        {
            //如果不被允许，则直接停止程序运行
            $res['status']=-1;
            $res['msg']='不允许上传改类型文件';
            $res['url']=$upload_path2.'/'.$filename;
            exit(json_encode($res));
        }


        //判断是否是通过HTTP POST上传的
        if(!is_uploaded_file($file['tmp_name']))
        {
            //如果不是通过HTTP POST上传的
            $res['status']=-1;
            $res['msg']='参数错误';
            $res['url']=$upload_path2.'/'.$filename;
            exit(json_encode($res));
        }

        //新文件名称
        $filename = $system_time."_".rand(0,100).".$filetype";
        $upload_path = "upfiles/"; //上传文件的存放路径

        //存放在临时文件夹，判断尺寸大小
        move_uploaded_file($file['tmp_name'],"data/cache/".$filename);
        $picsize = getimagesize("data/cache/".$filename);
        $imgsize = explode(" ",$picsize[3]);
        $width = preg_replace("/[^0-9]/","",$imgsize[0]);
        $height = preg_replace("/[^0-9]/","",$imgsize[1]);
        unset($picsize,$imgsize);
        $filesize = filesize("data/cache/".$filename);

        if($filesize>1048576) //1M
        {
            $FS->qgDelete("data/cache/".$filename);
            $res['status']=-1;
            $res['msg']='图片大小不能大于1M';
            $res['url']=$upload_path2.'/'.$filename;
            exit(json_encode($res));
        }



        //创建时间目录
        $folder1 = date("Ym",$system_time);
        $folder2 = date("d",$system_time);
        $upload_path1 = $upload_path."/".$folder1."/";
        $upload_path2 = $upload_path."/".$folder1."/".$folder2."/";
        //父级目录
        if(!file_exists($upload_path1))
        {
            mkdir($upload_path1,0777,true);
            chmod($upload_path1,0777);
        }
        else
        {
            chmod($upload_path1,0777);
        }
        //子级目录
        if(!file_exists($upload_path2))
        {
            mkdir($upload_path2,0777,true);
            chmod($upload_path2,0777);
        }
        else
        {
            chmod($upload_path2,0777);
        }

        //开始移动文件到相应的文件夹
        copy("data/cache/".$filename,$upload_path2.$filename);
        chmod($upload_path2.$filename,0777);

        $res['status']=1;
        $res['msg']='上传成功';
        $res['url']=$upload_path2.'/'.$filename;

        exit(json_encode($res));

        //删除临时文件
//        $FS->qgDelete("data/cache/".$filename);
//
//        $sql = "INSERT INTO ".$prefix."upfiles(userid,filetype,tmpname,filename,folder,postdate) VALUES('".$_SESSION["qg_sys_user"]["id"]."','".$filetype."','".$name."','".$filename."','".$upload_path2."','".$system_time."')";
//        $insert_id = $DB->qgInsert($sql);
//
//        if($act == "modifyok")
//        {
//            $sql = "UPDATE ".$prefix."msg set thumb='".$insert_id."' WHERE id='".$id."'";
//            $DB->qgQuery($sql);
//        }
    }





}
elseif($act=='msg_list'){//我的新闻列表


    $user_id=$_SESSION['qg_sys_user']['id'];
    $msg_list=$DB->qgGetAll("SELECT * FROM ".$prefix."msg WHERE user_id='".$user_id."' ORDER BY postdate DESC ");

	$msglist = array();
	$i = 1;
	foreach($msg_list AS $key=>$value)
	
	{
	
		$sql4 = "SELECT * FROM ".$prefix."category where id =".$value["cateid"];
		$rs4 = $DB->qgGetOne($sql4);
		
		$value["catename"] = $rs4["catename"];
	
	
		$sql2 = "SELECT * FROM ".$prefix."msg_content where id =".$value["id"];
		$rs3 = $DB->qgGetOne($sql2);
		preg_match_all("/src=\"\/?(.*?)\"/",$rs3["content"],$match);
		$value["filename"] = $match[1][0];

		$msglist[] = $value;
	
	}



    FOOT("my.msg_list");
}
elseif($act=="setting")
{
	FOOT("my.setting");
}

elseif($act=='addok'){//新闻添加入库（编辑）

    //新闻入库操作（写入公司新闻下的子分类里面，记住userid）
    if($_FILES['filename']){
        //编辑时如果有上传新图片，则删除旧图片(源文件)和图片数据库记录

        if($insert_id){
            $images= $DB->qgGetOne("SELECT * FROM ".$prefix."upfiles WHERE id=".$insert_id);
            $FS->qgDelete($images['folder'].$images['filename']);//删除旧图片(源文件)
            $DB->qgQuery("DELETE FROM ".$prefix."upfiles WHERE id='".$insert_id."'"); //删除图片数据库记录
        }
        include('class/my.uppic.class.php');
    }
    if($id>0){//编辑写入
        $DB->qgQuery("UPDATE ".$prefix."msg SET cateid='".$cateid."',subject='".$title."',postdate='".$system_time."',ifcheck='0',thumb='".$insert_id."' WHERE id='".$id."'");
        $DB->qgQuery("UPDATE ".$prefix."msg_content SET cateid='".$cateid."',content='".$content."' WHERE id='".$id."'");
        Error("修改成功",'my.php?act=msg_list');
    }else{
        //写入文章
        $data['cateid']=$cateid;//分类id
        $data['subject']=$title;//标题
        $data['postdate']=$system_time;//添加时间
        $data['ifcheck']=0;//审核状态
        $data['thumb']=$insert_id;//审核状态
        $data['user_id']=$_SESSION['qg_sys_user']['id'];
        $id=$DB->insert("msg",$data);

        //将文章内容入库
        $c_data['id']=$id;
        $c_data['cateid']=$_POST['cateid'];
        $c_data['content']=$_POST['content'];
        $DB->insert("msg_content",$c_data);
        //写入图片信息
        Error("添加成功",'my.php?act=msg_list');
    }

}
elseif($act=="change_pwd")//修改密码
{
//    $user_id=$_SESSION['qg_sys_user']['id'];
//    $user=$DB->qgGetOne("SELECT * FROM ".$prefix."user WHERE id=".$user_id);
//


    FOOT("my.change_pwd");
}
elseif($act=='change_pwdok'){//修改密码写入


    if($confirm_pwd!==$new_pwd){
        Error('两次密码不一致！', $_SERVER['HTTP_REFERER']);
    }
    $user_id=$_SESSION['qg_sys_user']['id'];
    $user=$DB->qgGetOne("SELECT * FROM ".$prefix."user WHERE id=".$user_id);

    if(md5($old_pwd)!==$user['pass']){
        Error('旧密码不正确，无法修改！', $_SERVER['HTTP_REFERER']);
    }

    if(md5($old_pwd)==md5($new_pwd)){
        Error('新密码不能同旧密码一样，修改失败！', $_SERVER['HTTP_REFERER']);
    }
    $DB->qgQuery("UPDATE ".$prefix."user SET pass='".md5($new_pwd)."'  WHERE id='".$user_id."'");
    Error('修改成功', 'my.php');
}
?>